package com.zhuhjay.app.web.controller.system;

import com.zhuhjay.common.base.controller.BaseController;
import com.zhuhjay.common.web.annotation.Matching;
import com.zhuhjay.common.web.enums.MatchingMode;
import com.zhuhjay.service.system.model.vo.DirInfoVo;
import com.zhuhjay.service.system.model.vo.FoldersVo;
import com.zhuhjay.service.system.service.SystemDirService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Zhuhjay
 * @since 2023-03-29
 */
@Api(tags = "文件系统的虚拟文件夹")
@Validated
@RestController
@RequestMapping("/dir")
public class SystemDirController extends BaseController {

    @Resource
    private SystemDirService dirService;

    @ApiOperation("创建文件夹")
    @PostMapping("{name}/{power}/{parentId}")
    @PreAuthorize("@perm.hasPerm('user:worker:fs:dir:create')")
    public void create(
            @PathVariable @Matching(MatchingMode.DirName) String name,
            @PathVariable @Matching(MatchingMode.DirPower) String power,
            @PathVariable @NotBlank(message = "父文件夹id不能为空") String parentId) {
        dirService.create(name, power, parentId);
    }

    @ApiOperation("删除文件夹")
    @DeleteMapping("{id}")
    @PreAuthorize("@perm.hasPerm('user:worker:fs:dir:delete')")
    public void delete(@PathVariable @NotBlank(message = "文件夹id不能为空") String id) {
        dirService.delete(id);
    }

    @ApiOperation("修改文件夹")
    @PutMapping("{id}/{name}/{power}")
    @PreAuthorize("@perm.hasPerm('user:worker:fs:dir:change')")
    public void change(
            @PathVariable @NotBlank(message = "文件夹id不能为空") String id,
            @PathVariable @Matching(MatchingMode.DirName) String name,
            @PathVariable @Matching(MatchingMode.DirPower) String power) {
        dirService.change(id, name, power);
    }

    @ApiOperation("获取文件夹信息")
    @GetMapping("{id}")
    public DirInfoVo get(@PathVariable @NotBlank(message = "文件夹id不能为空") String id) {
        return dirService.get(id);
    }

    @ApiOperation("文件夹列表")
    @GetMapping("list")
    public FoldersVo list() {
        return dirService.folderList();
    }

}
